package com.app.car.check.info;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.car.R;
import com.app.car.basis.BasisFragment;
import com.app.car.bean.RfidListData;
import com.app.car.greendao.GreenDaoUtils;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import my.TimeUtils;

/**
 * 茶青统计
 */
public class CheckInfoFragment extends BasisFragment {

    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private MyAdapterList mAdapter;
    private List<RfidListData> mListBean;
    private long time;
    private List<RfidListData> bookList;



    public static CheckInfoFragment newInstance(List<RfidListData> rfidList) {
        CheckInfoFragment fragment = new CheckInfoFragment();
        fragment.mListBean = rfidList;
        return fragment;
    }

    public static CheckInfoFragment newInstance(List<RfidListData> rfidList,long time,List<RfidListData> bookList) {
        CheckInfoFragment fragment = new CheckInfoFragment();
        fragment.mListBean = rfidList;
        fragment.bookList = bookList;
        fragment.time = time;
        return fragment;
    }


    @Override
    public void initViews() {
        setContentView(R.layout.check_info_fragment);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadingMoreEnabled(false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        setListView();
    }


    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        setEmptyView();
    }


    private void setEmptyView() {
        if (mListBean == null) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
            findViewById(R.id.refresh_view).setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            setListView();
                        }
                    });
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }



    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyAdapterList.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.check_info_list_item, viewGroup, false);
            return new MyAdapterList.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyAdapterList.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_index)
            public TextView text_index;
            @BindView(R.id.text_rfid)
            public TextView text_rfid;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_gid)
            public TextView text_gid;
            @BindView(R.id.text_time)
            public TextView text_time;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                RfidListData data = mListBean.get(position);

                if (position % 2 == 0) {
                    list_item.setBackground(getResources().getDrawable(R.color.ooo));
                }else {
                    list_item.setBackground(getResources().getDrawable(R.color.white));
                }

                text_index.setText(""+(position+1));
                text_rfid.setText(""+data.getRfid());
                text_gid.setText(""+data.getGid());
                if(time != 0){
                    text_time.setText(TimeUtils.getTimeLongToStrByFormat(time, "yyyy-MM"));
                }else {
                    text_time.setText(""+data.getYear()+"-"+data.getMonth());
                }
                if(bookList!= null){
                    for (RfidListData data1 : bookList){
                        if(data1.getRfid().equals(data.getRfid())){
                            text_name.setText(""+data1.getName());
                            break;
                        }
                    }
                }else {
                    text_name.setText("");
                }
            }

        }
    }

}
